<?php
class Item {
	
	private $items = array();
	private $where;
	private $table;
	private $seo_url;
	public  $title;
	public function __construct($tbl) {
		$this->table= $tbl;
	}
	
	public function init($column = " * ",$paging="true"){
		$this->column = $column;
		if($paging){
			$limit = $this->make_paging();	
			$this->search($limit);	
		}
		else{
			$this->search('0,100');
		}
	}
		
	
	public function search($limit = ''){
	global $db;
		
		$sql = "SELECT $this->column FROM ". $this->table . " $this->where $this->order LIMIT $limit ;";		
		$stmt = $db->prepare($sql)->execute();
				
		while($row = $stmt->fetch_assoc()){
				$this->items[] = $row;
		}		
	}
	
/**	
	public function search($column = " * "){
	global $db;

		$sql = "SELECT $column FROM ". $this->table . " $this->where ;";
//		/echo $sql;
		$stmt = $db->prepare($sql)->execute();		
		while($row = $stmt->fetch_assoc()){
			// only for one column
			if(count($row) == 1){
				$this->items[] = $row[$column];				
			}
			else{
				$this->items[] = $row;
				
			}			
		}		
	}*/
	
	public function getTitle($what = array('model','znacka')){
		$title = '';
		
		if(count($this->items) === 1){
			foreach($what as $val){
				$title .= $this->items[0][$val]. " ";			
			}			
			return trim($title);
		}
		return 'Auto Říčný, prodej a nákup bouraných a ojetých automobilů a motocyklů';
	}
	
	
	public function getDesc($what = array('model','znacka')){
		$title = '';
		
		if(count($this->items) === 1){
			foreach($what as $val){
				$title .= $this->items[0][$val]. " ";			
			}			
			return " | ".trim($title);
		}
		return '';
	}	
	
	
	public function addOrder($or){
		
			$this->order = $or;
	}
	
	
	
	
	
	
	
	
	public function addwhere($where){
		global $db;
		if(is_array($where)){
			$this->where = " WHERE ";
			foreach($where as $column=>$value){
				$this->where .= (gettype($value)=='string') ?  " {$column}='$value' " : " {$column}=$value ";				 		
			}
		}
		else if($where='random'){
						
		}
		else{
			$this->where = '';
		}
	}
	// create a href element for items 
	public function create_ref(){
		
		foreach($this->items as $key=>$text){
			$this->items[$key] = "<a href='/galerie/$text' target='_blank'><img src='/galerieTH/$text alt='$text' /></a>";
			
		}
		
	}
	
	private function set_url(){
		
		foreach($this->items as $key=>$text){
			$url = str_replace(' ','-',$this->items[$key]['znacka'] . ' '. $this->items[$key]['model']);
			$this->items[$key]['seo_url'] = $url; 			
		}
		//print_r($this->items);
	}
	
	/**
	* Paging rountine
	* sets paging for every search and controls, if there is something to output as well
	*/
	public function make_paging()
	{
		global $user, $smarty, $db;
		
		// total_items
		$sql = 'SELECT COUNT(*) as count
				FROM ' .$this->table .'
				 ' . $this->where;		
		
		$stmt = $db->prepare($sql)->execute();		
		$row = $stmt->fetch_assoc();
		//$db->sql_freeresult($result);
		$total_items = (int) $row['count'];

		if (!$total_items)
		{ // nothing to output
			return '0, 0';
		}

		// additional
		$ipp		= request_var('ipp',5);
		$page		= request_var('pg', 0);
		$last_page	= ceil($total_items / $ipp);
		
		// controls page
		$page = max(min($page, $last_page), 0);

		// pages
		$show_pages = 5;
		$start_page = $page > 2 ? $page - 2 : 0;
		$end_page = $page > 2 ? $page + 3 : $show_pages;
		
		if ($end_page > $last_page)
		{ // kontroluje posledni
			$end_page = $last_page;
		}

		$diff = $end_page - $page;
		if ($diff < 3)
		{ // kontroluje prvni
			$start_page -= 3 - $diff;

			if ($start_page < 0)
			{ // kontrola podteceni
				$start_page = 0;
			}
		}

		$path = $user->page['page'];
		//print_r($user);		
		// remove &pg if exists
		$path = ereg_replace('&pg=[0-9]+', '', $path);
		$path ="/".$path;
		// ulozi stranky a pocet
		$pg = '';

		if ($start_page != 0)
		{ // zobrazi prvni str
			$pg .= sprintf("<a href=\"%s\" %s>%d</a>-", $path . '&pg=0', 0 == $page ? 'class="selected"' : '', 1);
		}

		for ($i = $start_page; $i < $end_page; $i++)
		{
			$pg .= sprintf("<a href=\"%s\" %s>%d</a>", $path . '&pg=' . $i, $i == $page ? 'class="selected"' : '', $i + 1);
		}

		if ($end_page < $last_page)
		{ // zobrazi posledni str
			$i = $last_page - 1;
			$pg .= sprintf("-<a href=\"%s\" %s>%d</a>", $path . '&pg=' . $i, $i == $page ? 'class="selected"' : '', $i + 1);
		}

		$per_page = array(5, 10, 20, 50, 100); // options

		if (!in_array($ipp, $per_page))
		{
			$ipp = 5;
		}

		// vyber items_per_page
		$items = '';
		for ($i = 0, $count = sizeof($per_page); $i < $count; $i++)
		{
			$items .= sprintf("<option value=\"%d\" %s>%d</option>\n", $per_page[$i], $ipp == $per_page[$i] ? 'selected="selected"' : '', $per_page[$i]);
		}

		$pg = str_replace('&amp;', '&', $pg); // zmeni &amp; na &
		$pg = str_replace('&', '&amp;', $pg); // odstrani duplicitni &amp;
		if(empty($_GET)) $pg = str_replace('&amp;', '?', $pg);  //kdyz neni get parametr
				
		$smarty->assign(array(
			'O_PAGES'		=> $pg,
			'O_IPP'			=> $items,
			'TOTAL_ITEMS'	=> $total_items,
		));

		// returns limit for output
		return ($page * $ipp . ', ' . $ipp);
	}	
	
		
	
	public function assign($exp='item',$table = false){
		global $smarty;
	 	$this->set_url();
		if($table){
			$smarty->assign( $exp, $this->items);
			$smarty->assign( 'tr', array('bgcolor="#eeeeee"','bgcolor="#dddddd"') );			
		}
		else{
			$smarty->assign($exp,$this->items);	
			
			if(count($this->items)){
				$smarty->assign('more_photo',1);	
			}
			else{
				$smarty->assign('more_photo',0);
			}
		}
	}
	
}
?>